<template>
  <div class="wrapLimit clx main">
    <div class="reset">

      <el-form class="ruleForm" label-width="100px" :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="原密码：" prop="currentPassword">
          <el-input type="password" autocomplete="off" v-model="ruleForm.currentPassword" maxlength="20"
            placeholder="原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPassword">
          <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码：" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="再次输入新密码"></el-input>
        </el-form-item>
        <div style="padding-left: 40%;">
          <el-button type="primary" @click="handleSubmit">确定</el-button>
          <el-button type="info" @click="close()">返回</el-button>
        </div>
      </el-form>

    </div>
  </div>
</template>

<script>


import { mapGetters } from 'vuex'
import { changePassWord } from '@/api/user'
export default {
  name: 'Reset',
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }
      else {
        callback();
      }
    };

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value.length < 6) {
        callback(new Error('密码长度至少为6位'));
      }
      else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入的新密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newPassword: '',
        checkPass: '',
        currentPassword: '',
      },
      rules: {
        currentPassword: [
          { validator: validateOldPass, trigger: 'blur' }
        ],
        newPassword: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
  },
  watch: {
  },
  created() {

  },
  mounted() {
  },
  destroyed() {

  },
  computed: {
    ...mapGetters([
      'userName'
    ])
  },
  methods: {
    close() {
      this.$router.go(-1)
    },
    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.userName == "admin") {
            this.$message({
              message: "管理员账号不可修改",
              type: "error",
            });
          } else {
            changePassWord(this.ruleForm).then((res) => {
              this.$alert("密码修改成功，将退出重新登录", "提示", {
                showClose: false,
                confirmButtonText: "确定",
                callback: async (action) => {
                  await this.$store.dispatch('user/logout');
                  this.$router.push(`/login?redirect=/home`);
                },
              });
            })
          }
        }
      });
    },
  },
}
</script>
<style lang="scss" >
* {
  margin: 0;
  padding: 0;
}

.reset {
  min-height: 100vh;
  padding: 1px;

  .ruleForm {
    width: 400px;
    // height: 200px;
    /* 水平垂直居中 */
    margin: 250px auto;

    ::v-deep .el-form-item__label {
      line-height: 50px
    }

    ::v-deep .el-form-item__content {
      line-height: 50px
    }
  }

}
</style>
